package com.face.face.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.face.face.AppContext;
import com.face.face.R;
import com.face.face.entity.Attest;
import com.face.face.task.AttestTask;
import com.face.face.utils.ServiceUtil;
import com.poya.bankcertificationsdk.model.constant.ExternalConstant;
import com.poya.bankcertificationsdk.view.activity.TakeFaceActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class RzActivity extends BaseActivity implements View.OnClickListener {

    private final static int SIGN_CODE_03 = 9003;
    private ImageView head_img;
    private TextView rz;
    private EditText uid;
    private TextView name;
    private ImageView del_name;
    private ImageView del_uid;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rz);

        rz = (TextView) findViewById(R.id.rz);
        uid = (EditText) findViewById(R.id.uid);
        name = (TextView) findViewById(R.id.name);
        del_uid = (ImageView) findViewById(R.id.del_uid);
        head_img = (ImageView) findViewById(R.id.head_img);
        del_name = (ImageView) findViewById(R.id.del_name);
        back = (ImageView) findViewById(R.id.back);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String imagePath = bundle.getString("imagePath");
            showImage(imagePath);
        }

        back.setOnClickListener(this);
        rz.setOnClickListener(this);
        del_name.setOnClickListener(this);
        del_uid.setOnClickListener(this);
        head_img.setOnClickListener(this);

        uid.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    del_uid.setVisibility(View.INVISIBLE);
                } else {
                    del_uid.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void showImage(String imagePath) {
        if (imagePath == null) {
            head_img.setImageBitmap(null);
            return;
        }
        FileInputStream fis;
        try {
            fis = new FileInputStream(imagePath);
            Bitmap bmp = BitmapFactory.decodeStream(fis);
            fis.close();
            head_img.setImageBitmap(bmp);
            ServiceUtil.getInstance().setValue(AppContext.getInstance().bitmapToBase64(bmp));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rz:
                rz();
                break;
            case R.id.del_name:
                del_name();
                break;
            case R.id.del_uid:
                del_uid();
                break;
            case R.id.head_img:
                head_img();
                break;
            case R.id.back:
                onBackPressed();
                break;
        }
    }

    private void head_img() {
        TakeFaceActivity.startActivityForResult(this, SIGN_CODE_03);
    }

    private void del_name() {
        name.setText("");
    }

    private void del_uid() {
        uid.setText("");
    }

    private void rz() {
        String uid_str = uid.getText().toString();
        if (TextUtils.isEmpty(uid_str)) {
            show("证件号不能为空");
            return;
        }
        showLoadingDialog();
        new AttestTask(this, handler, uid_str).execute();
    }

    private void show(String msg) {
        setMsg(msg);
        showMessageDialog();
        uid.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissMsgDialog();
            }
        }, 2000);
    }

    private void attest_finish(Object obj) {
        if (obj == null) return;
        if (obj instanceof Attest) {
            Attest attest = (Attest) obj;
            String code = attest.getResult_code();
            if ("ok".equals(code)) {
                show(attest.getAuth_message());
                String namestr = attest.getName();
                if (TextUtils.isEmpty(namestr)) {
                    name.setText("");
                } else {
                    name.setText(namestr);
                }
            } else if ("error".equals(code)) {
                show(attest.getResult_message());
                name.setText("");
            }
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dismissLoadingDialog();
            switch (msg.what) {
                case BaseActivity.SUCCESS:
                    attest_finish(msg.obj);
                    break;
                case BaseActivity.FAILED:
                    show("请求失败");
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SIGN_CODE_03) {
            boolean success = data.getBooleanExtra(ExternalConstant.TAKE_IMAGE_SUCCESS_KEY, false);
            if (success) {
                String imagePath = data.getStringExtra(ExternalConstant.FACE_IMAGE_PATH_KEY);
                showImage(imagePath);
            }
        }
    }
}
